package com.noble.meether.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.noble.meether.R;
import com.noble.glamour.bean.center.user.detail.UserDetail;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.LoadingDialog;
import com.noble.glamour.module.util.UIShow;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.library.camera.ImgSelectUtil;
import com.noble.library.image.ImageLoader;
import com.noble.library.log.PToast;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.observe.PObserver;
import com.noble.meether.MeetUIShow;
import com.noble.meether.logic.center.EditKey;

import java.util.HashMap;

/**
 * 编辑个人资料页
 */
public class UserEditInfoAct extends BaseAppCompatActivity implements PObserver, View.OnClickListener, ImgSelectUtil.OnChooseCompleteListener {
    private TextView name, sex;        //基本资料
    private ImageView user_head;

    private UserDetail userDetail;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.userinfo_layout);
        initView();
        initData();
        refreshView();
        MsgMgr.getInstance().attach(this);
    }

    private void initView() {
        setBackView("个人信息");
        name = findViewById(R.id.name);
        sex = findViewById(R.id.sex);
        user_head = findViewById(R.id.user_head);
    }

    private void initData() {
        findViewById(R.id.name_view).setOnClickListener(this);
        findViewById(R.id.sex_view).setOnClickListener(this);
        findViewById(R.id.head_view).setOnClickListener(this);
    }

    public void refreshView() {
        userDetail = ModuleMgr.getCenterMgr().getMyInfo();
        ImageLoader.loadCircleAvatar(this, userDetail.getAvatar(), user_head);
        name.setText(userDetail.getNickname());
        sex.setText(userDetail.isMan() ? "男" : "女");
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_MyInfo_Change:
                refreshView();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }

    //=================== 内容修改 ===============================
    private boolean isUpdate = false;

    private void setIsUpdate() {
        isUpdate = true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == EditKey.EDIT_REQUEST_CODE) {
            HashMap<String, Object> postParams = new HashMap<>();
            switch (resultCode) {
                case EditKey.UPDATE_NICK_NAME:
                    setIsUpdate();
                    postParams.put(EditKey.s_key_nickName, data.getStringExtra("newValue"));
                    break;

                case EditKey.UPDATE_CONNECTION_PHONE:
                    setIsUpdate();
                    postParams.put(EditKey.s_key_mobile, data.getStringExtra("newValue"));
                    postParams.put(EditKey.s_key_mobileAuth, data.getIntExtra("newAuth", 0));
                    break;

                case EditKey.UPDATE_CONNECTION_QQ:
                    setIsUpdate();
                    postParams.put(EditKey.s_key_qq, data.getStringExtra("newValue"));
                    postParams.put(EditKey.s_key_qqAuth, data.getIntExtra("newAuth", 0));
                    break;

                case EditKey.UPDATE_CONNECTION_WECHAT:
                    setIsUpdate();
                    postParams.put(EditKey.s_key_wechat, data.getStringExtra("newValue"));
                    postParams.put(EditKey.s_key_wechatAuth, data.getIntExtra("newAuth", 0));
                    break;
            }
            if (isUpdate) {
                isUpdate = false;
                LoadingDialog.show(this, "正在修改，请稍候...");
                ModuleMgr.getCenterMgr().updateMyInfo(postParams, null);
            }
        }
    }

    @Override
    public void onClick(View v) {
        final UserDetail userDetail = ModuleMgr.getCenterMgr().getMyInfo();
        switch (v.getId()) {
            case R.id.head_view:
                MeetUIShow.showImgSelectDialog(this, this);
                break;

            case R.id.name_view:
                UIShow.showEditContentAct(this, userDetail.getNickname(),
                        EditKey.EDIT_NICK_NAME);
                break;

            case R.id.sex_view:
                PToast.showShort("性别不能修改");
                break;
        }
    }

    @Override
    public void onUrl(final String url) {
        LoadingDialog.show(this, "正在上传头像");
        ModuleMgr.getCenterMgr().uploadAvatar(url, null);
    }
}
